home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Devices / Qwertytunes / Qwertytunes Filing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-03  |  3.5 KB  |  214 lines  |  [TEXT/KAHL]

  1. /*
  2.  * file: Qwertytunes Filing.c
  3.  *
  4.  * started 22 January 1992
  5.  * david van brink
  6.  *
  7.  * The filing routines
  8.  *
  9.  */
  10.  
  11.  
  12. /*--------------------------
  13.     Inclusions
  14. --------------------------*/
  15.  
  16. #include <Files.h>
  17. #include <StandardFile.h>
  18. #include <Resources.h>
  19. #include <Errors.h>
  20. #include <Memory.h>
  21.  
  22. #include "BigEasy2.h"
  23. #include "BigEasyDialogs.h"
  24.  
  25. #include "Qwertytunes.h"
  26. #include "Qwertytunes Filing.h"
  27.  
  28. /*--------------------------
  29.     Local Prototypes
  30. --------------------------*/
  31.  
  32. OSErr InternalSaveData(TDoc *d);
  33.  
  34. /*--------------------------
  35.     Wasabe
  36. --------------------------*/
  37.  
  38. void OpenDoc(short n,short item, short ref)
  39.     {
  40.     StandardFileReply sfr;
  41.     OSType ourType[5];
  42.     ourType[0] = kDocumentFileType;
  43.     StandardGetFile(nil,1,ourType,&sfr);
  44.  
  45.     if(sfr.sfGood)
  46.         {
  47.         OpenDocSpec(&sfr.sfFile);
  48.         }
  49.     }
  50.  
  51.  
  52. void OpenDocSpec(FSSpec *fSpec)
  53.     {
  54.     short i;
  55.     short refNum;
  56.     register TDoc *d;
  57.     TSaveRecord **sr;
  58.     FInfo fileInfo;
  59.  
  60.     for(i = 0; i<kDocMax; i++)
  61.         {
  62.         d = &gDoc[i];
  63.         if(!d->used)
  64.             goto gotOne;
  65.         }
  66.  
  67.     EasyDialogMessage(0,(StringPtr)0x910,"\pNo more windows may be opened.",
  68.             kEasyDialogOkay);
  69.     goto goHome;
  70.  
  71. gotOne:
  72.     refNum = FSpOpenResFile(fSpec,fsRdPerm);
  73.     if(refNum == 1)
  74.         goto fileError;
  75.     sr = (void *)Get1IndResource(kDocumentResType,1);
  76.     if(!sr)
  77.         goto closeAndFileError;
  78.     HLock((void *)sr);
  79.     if((**sr).docVersion != kDocVersion)
  80.         {
  81.         EasyDialogMessage(0,fSpec->name,
  82.                 "\pThat was an old version of the file format.",
  83.                 kEasyDialogOkay);
  84.         goto closeAndFileError;
  85.         }
  86.  
  87.     d->used = true;
  88.     d->docSpec = *fSpec;
  89.     NewDocFromSaveRecord(i,&(**sr));
  90.  
  91.     CloseResFile(refNum);
  92.     goto goHome;
  93.  
  94. closeAndFileError:
  95.     CloseResFile(refNum);
  96. fileError:
  97.     EasyDialogMessage(0,fSpec->name,
  98.             "\pThere was a problem opening the file.",
  99.             kEasyDialogOkay);
  100.  
  101. goHome:;
  102.     }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. short SaveDoc(short n,short item, short ref)    
  109. /*
  110.  * return nonzero to
  111.  * say it was cancelled.
  112.  */
  113.     {
  114.     TDoc *d;
  115.     short result;
  116.  
  117.     d = &gDoc[n - kFirstDocWindow];
  118.  
  119.     DeactivateDoc(n);
  120.  
  121.     if(!d->everSaved)
  122.         result = SaveAsDoc(n,item,ref);
  123.     else
  124.         result = InternalSaveData(d);
  125.  
  126.     ActivateDoc(n);
  127.     return result;
  128.     }
  129.  
  130. short SaveAsDoc(short n,short item, short ref)
  131.     {
  132.     StandardFileReply sfr;
  133.     register TDoc *d;
  134.     Boolean result;
  135.     TSaveRecord *sr,**srH;
  136.     short refNum;
  137.     OSErr thisError;
  138.     OSType fileType;
  139.  
  140.     d = &gDoc[n - kFirstDocWindow];
  141.  
  142.     DeactivateDoc(n);
  143.  
  144.     StandardPutFile("\p",d->docSpec.name,&sfr);
  145.  
  146.     result = !sfr.sfGood;
  147.     if(sfr.sfGood)
  148.         {
  149.         d->docSpec = sfr.sfFile;
  150.         SetWTitle(d->w,d->docSpec.name);
  151.         thisError = InternalSaveData(d);
  152.         }
  153.     else
  154.         thisError = 1;
  155.  
  156.     return thisError;
  157.     }
  158.  
  159.  
  160. OSErr InternalSaveData(TDoc *d)
  161.     {
  162.     TSaveRecord **srH,*sr;
  163.     short refNum;
  164.     OSErr thisError;
  165.     Rect r;
  166.  
  167.     FixUpDocConnectionList(d);
  168.  
  169.     srH = (void *)NewHandle(sizeof(TSaveRecord));
  170.     HLock((void *)srH);
  171.     sr = *srH;
  172.  
  173.     sr->docVersion = kDocVersion;
  174.     SetPort(d->w);
  175.     sr->windowRect = qd.thePort->portRect;
  176.     LocalToGlobal((Point *)&sr->windowRect.top);
  177.     LocalToGlobal((Point *)&sr->windowRect.bottom);
  178.     sr->sr = d->sr;
  179.  
  180.     HUnlock((void *)srH);
  181.  
  182.     FSpCreateResFile(&d->docSpec,kCreatorFileType,kDocumentFileType,0);
  183.  
  184.     thisError = ResError();
  185.     if(thisError == dupFNErr)
  186.         thisError = 0;
  187.     if(thisError)
  188.         goto goHome;
  189.  
  190.     refNum = FSpOpenResFile(&d->docSpec,fsRdWrPerm); 
  191.  
  192.     if(refNum != -1)
  193.         {
  194.         Replace1Resource((void *)srH,kDocumentResType,10);
  195.         CloseResFile(refNum);
  196.         d->changed = false;
  197.         d->littleChanged = false;
  198.         d->everSaved = true;
  199.         }
  200.     else
  201.         thisError = -1;
  202.  
  203. goHome:
  204.     if(srH)
  205.         {
  206.         HUnlock((Handle)srH);
  207.         DisposeHandle((Handle)srH);
  208.         }
  209.  
  210.     FixUpMenus(d);
  211.     return thisError;
  212.     }
  213.  
  214.